var layers = [];  		//Holds the initialized layers.
var layersActive = [];	//Holds the visible layers.
var layersConfig = [];
var infowindowGraphicsLayer = [];

function initLayerLoader() {
	dojo.xhrGet({
	    // The URL to request
	    url: "/data/layers.json.php",
	    handleAs: "json",
	    // The method that handles the request's successful result
	    // Handle the response any way you'd like!
	    load: gotLayersJSON
	});
}
function gotLayersJSON(layersJSON) {
	console.log('Got layers.json.');
	dojo.forEach(layersJSON, function(layer) {
		layersConfig[layer.id] = layer;
	//	layers[layer.id] = initJSONLayer(layer);
		if (layer.id == 'basemap') {
			addLayer('basemap');
			}
	});
}
function initJSONLayer(json) {
	
	// Verify value for opacity, default to 1 (full opacity) if there are errors
	if (typeof json.opacity == undefined || isNaN(json.opacity) || json.opacity == null) {
		json.opacity = 1;
	}
	
	if (json.type == 'dynamic') {
		var lyr = new esri.layers.ArcGISDynamicMapServiceLayer(json.url, {id:json.id, opacity:json.opacity});
		}		
	if (json.type == 'featureservice') {
		dojo.require("esri.layers.FeatureLayer");
		var lyr = new esri.layers.FeatureLayer(json.url, {id:json.id, opacity:json.opacity, mode: esri.layers.FeatureLayer.MODE_ONDEMAND});
		}
	if (json.type == 'cached') { 
		var lyr = new esri.layers.ArcGISTiledMapServiceLayer(json.url, {id:json.id, opacity:json.opacity});
		}
	if (json.type == 'kml') { 
		var jsonURL = json.url+'?nocache='+(new Date).getTime();
		var lyr = new esri.layers.KMLLayer(jsonURL, {id:json.id, opacity:json.opacity}); 
		}
	
	console.log('Layer "'+json.id+'" initialized from layers.json."');

	return lyr;
	
}
function initLayerPopup(layer, infowindow) {
		infowindowGraphicsLayer[layer] = new esri.layers.GraphicsLayer();
		
		
		//build query filter
		if (parseInt(layersConfig[layer].infowindow[infowindow].layerID) >= 0) {
			var parkingQueryTask = new esri.tasks.QueryTask(layersConfig[layer].url + "/" + layersConfig[layer].infowindow[infowindow].layerID);
		}
		else {
			var parkingQueryTask = new esri.tasks.QueryTask(layersConfig[layer].infowindow[infowindow].layerID);
		}
		
		var parkingQuery = new esri.tasks.Query();
		parkingQuery.returnGeometry = true;
		parkingQuery.outFields = ["*"];
		parkingQuery.where = "1=1";
		parkingQuery.geometryPrecision = 1;
		//parkingQuery.geometry = evt.mapPoint;  //query the clicked point

		var infoTemplate = new esri.InfoTemplate();
		infoTemplate.setTitle(layersConfig[layer].infowindow[infowindow].infowindowTitle);
		infoTemplate.setContent(layersConfig[layer].infowindow[infowindow].infowindowContent);

		dojo.connect(parkingQueryTask, "onComplete", function(featureSet4) {

			//QueryTask returns a featureSet.  Loop through features in the featureSet and add them to the map.
			for (var i=0, il=featureSet4.features.length; i<il; i++) {			
				//Get the current feature from the featureSet, Feature is a graphic
				var graphic4 = featureSet4.features[i];
				if (featureSet4.geometryType == 'esriGeometryPolygon') {
					var symbolParking = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,0,0])), new dojo.Color([0,0,128,0]));
					var highlightSymbolParking = new esri.symbol.SimpleFillSymbol(esri.symbol.SimpleFillSymbol.STYLE_SOLID, new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID, new dojo.Color([0,0,128]), 2), new dojo.Color([0,0,128,0.4]));				
				}
				if (featureSet4.geometryType == 'esriGeometryPoint') {
					var symbolParking = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 30,new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,   new dojo.Color([0,0,0, 0]), 2),   new dojo.Color([255,255,255,0]));
					var highlightSymbol = new esri.symbol.SimpleMarkerSymbol(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE, 30,new esri.symbol.SimpleLineSymbol(esri.symbol.SimpleLineSymbol.STYLE_SOLID,   new dojo.Color([0,0,0, 0]), 2),   new dojo.Color([255,255,255,0]));
				}	
				graphic4.setSymbol(symbolParking);
				graphic4.setInfoTemplate(infoTemplate);
				//Add graphic to the floor graphics layer.
				infowindowGraphicsLayer[layer].add(graphic4);
			}
			map.addLayer(infowindowGraphicsLayer[layer]);
			map.graphics.enableMouseEvents();
			//click on infowindowGraphicsLayer[layer] creates new graphic with geometry from the event.graphic and adds it to the maps graphics layer
			dojo.connect(infowindowGraphicsLayer[layer], "onClick", function(evt) {
				//map.infoWindow.resize(350,250);
				map.graphics.clear();  //use the maps graphics layer as the highlight layer
				var content = evt.graphic.getContent();
				var title = evt.graphic.getTitle();
                                //dojo.byId("mapInfowindowDiv").style.visibility = 'visible';
                                //dojo.byId("mapInfowindowDiv").innerHTML = title + '<hr />' + content;
                                if (user_device == 'mobile' || user_device == 'tablet') {
                                    map.infoWindow.setFeatures([evt.graphic]);                     
                                }    
            
            
				//var highlightGraphic = new esri.Graphic(evt.graphic.geometry,highlightSymbolParking);
				//map.graphics.add(highlightGraphic);
			});
			dojo.connect(infowindowGraphicsLayer[layer], "onMouseOver", function() {
				map.setCursor("pointer");
			});
			dojo.connect(infowindowGraphicsLayer[layer], "onMouseOut", function() {
				map.setCursor("default");
			});
		});
		parkingQueryTask.execute(parkingQuery);
}